import Qt 4.7
import "context"
Rectangle {
    id:mainWindow
    width: 320
    height: 360
    color:"lightGrey"
    property ContactListTab contactList
    TabWidget
    {
        id:tabwidget
        width: mainWindow.width
        anchors.top: mainWindow.top;anchors.left: mainWindow.left;anchors.bottom:loginName.top
    }


    Row
    {
        id:loginName
        height:30
        width:parent.width
        visible:false
        anchors {left:parent.left;bottom:buttons.top;bottomMargin:5}
        Text
        {
            id:loginLabel
            text:"nicky name:"
            wrapMode:Text.Wrap
        }
        Rectangle
        {
            color:"white"
            anchors {left:loginLabel.right}
            width:parent.width-loginLabel.width-ok.width-20
            height:parent.height
                TextEdit
                {
                    id:loginNameInput
                    width:parent.width
                    height: parent.height
                    anchors.left:parent.left
                }
        }
        TextButton
        {
            id:ok
            text:"ok"
            onClicked:{ login(loginNameInput.text);loginName.visible=false}
            anchors.right:parent.right
        }

    }

    // add two buttons to test the function
    Row
    {
        id:buttons
        anchors { left: parent.left;right:parent.right; bottom: parent.bottom; margins: 20 }
        spacing: 10
        TextButton
        {
            text: "set"
            onClicked:
            {
                loginName.visible = true
            }
        }
        TextButton
        {
            text: "close current tab"
            anchors.right:parent.right
            onClicked: tabwidget.closeCurrentTab()
        }
    }

    Component.onCompleted:
    {
        console.log("component initial")

        var component = Qt.createComponent("context/ContactListTab.qml");
        contactList = component.createObject(tabwidget);
        contactList.rowDbClicked.connect(onContactRowDbClicked)
        contactList.model = contactNamesModel

        tabwidget.addTab("contact","contact",contactList)
        tabwidget.tabClosed.connect(stopChating)
    }

    function onContactRowDbClicked(ip,name)
    {
        if(!ip || !name ) return;
        var tab =tabwidget.addChatTab(ip,name)
        if(tab) tab.sendMessage.connect(mainWindow.sendMessage)
    }

    signal login(string username)
    signal sendMessage(string ip,string data)
    signal stopChating(string ip)
    signal closeWindow()

    function onMessageUpdated(ip,name,data)
    {

        console.log("message update in qml")
        var tab = tabwidget.getTab(ip)
        if(!tab)
        {
            tab = tabwidget.addChatTab(ip,name)
            if(tab) tab.sendMessage.connect(mainWindow.sendMessage)
        }
        tab.messageUpdate(data)
    }


    Connections {
        target: logicAdaptor
        onMessageUpdated: mainWindow.onMessageUpdated(ip,name,data)
    }
}
